package com.blocks57.homework.controller;

import com.blocks57.homework.dto.ProductDTO;
import com.blocks57.homework.dto.SuccessResponseDTO;
import com.blocks57.homework.exception.DataNotFoundException;
import com.blocks57.homework.exception.DuplicateElementException;
import com.blocks57.homework.model.ShoppingCartItem;
import com.blocks57.homework.dto.ShoppingCartItemDTO;
import com.blocks57.homework.service.ShoppingCartItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ShoppingCart Controller
 *
 * Created by Tom at 2023/10/29.
 */
@RestController
@RequestMapping("/shoppingCart")
@Api("ShoppingCart Related Operation")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartItemService shoppingCartItemService;
    @ApiOperation(value = "Add Shopping Cart Item", notes = "This api is used to add a cart item", httpMethod = "POST")
    @ApiResponses(value = { @ApiResponse(code = 500, message = "Internal Server error"),
            @ApiResponse(code = 401, message = "UnAuthorized") })
    @PostMapping(value = "/addCartItem/{userId}", consumes = "application/json", produces = "application/json")
    public ResponseEntity<?>  addProductItem(@RequestBody ShoppingCartItemDTO shoppingCartItemDTO,
                                           @PathVariable("userId") Long userId) throws DuplicateElementException {

        return new ResponseEntity<SuccessResponseDTO>(new SuccessResponseDTO(shoppingCartItemService.saveShoppingCartItem(shoppingCartItemDTO, userId)), HttpStatus.CREATED);
    }

    @ApiOperation(value = "Get All Shopping Cart Items", notes = "this api is used to get all shopping cart items", httpMethod = "GET")
    @ApiResponses(value = { @ApiResponse(code = 500, message = "Internal Server error"),
            @ApiResponse(code = 401, message = "UnAuthorized") })
    @GetMapping(value = "/getAllCartItems/{userId}",produces = "application/json")
    public ResponseEntity<?>  getAllItems(@PathVariable("userId") Long userId){

        return new ResponseEntity<List<ShoppingCartItemDTO>>(shoppingCartItemService.findAll(userId), HttpStatus.OK);

    }

    @ApiOperation(value = "Update Shopping Cart Item", notes = "this api is used to update shopping cart item", httpMethod = "PUT")
    @ApiResponses(value = { @ApiResponse(code = 500, message = "Internal Server error"),
            @ApiResponse(code = 401, message = "UnAuthorized") })
    @PutMapping(value = "/updateCartItem/{userId}", consumes = "application/json", produces = "application/json")
    public ResponseEntity<?>  updateProductItem(@RequestBody ShoppingCartItemDTO shoppingCartItemDTO,
                                              @PathVariable("userId") Long userId) throws DataNotFoundException {

        return new ResponseEntity<SuccessResponseDTO>(new SuccessResponseDTO(shoppingCartItemService.updateShoppingCartItem(shoppingCartItemDTO, userId)), HttpStatus.ACCEPTED);

//        return shoppingCartItemService.updateShoppingCartItem(shoppingCartItemDTO, userId);
    }

    @ApiOperation(value = "Delete Shopping Cart Item", notes = "this api is used to delete shopping cart item by Id", httpMethod = "DELETE")
    @ApiResponses(value = { @ApiResponse(code = 500, message = "Internal Server error"),
            @ApiResponse(code = 401, message = "UnAuthorized") })
    @DeleteMapping(value = "/deleteShoppingCartItem/{userId}/{shoppingCartItemId}", produces = "application/json")
    public ResponseEntity<?>  deleteProductItem(@PathVariable("shoppingCartItemId") Long shoppingCartItemId,
                                  @PathVariable("userId") Long userId) throws DataNotFoundException {

        return new ResponseEntity<SuccessResponseDTO>(new SuccessResponseDTO(shoppingCartItemService.deleteShoppingCartItem(shoppingCartItemId, userId)), HttpStatus.OK);
    }

    @ApiOperation(value = "Clear Shopping Cart", notes = "this api is used to delete all shopping cart items from shopping cart", httpMethod = "DELETE")
    @ApiResponses(value = { @ApiResponse(code = 500, message = "Internal Server error"),
            @ApiResponse(code = 401, message = "UnAuthorized") })
    @DeleteMapping(value = "/clearShoppingCart/{userId}", produces = "application/json")
    public ResponseEntity<?> ClearCart(@PathVariable("userId") Long userId) {

        return new ResponseEntity<SuccessResponseDTO>(new SuccessResponseDTO(shoppingCartItemService.clearShoppingCart(userId)), HttpStatus.OK);
    }

}
